import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.util.ArrayList;

/**
 * @author lys
 * 更新书籍页面
 */

public class UpdateBook extends JFrame {
    private static UpdateBook instance;
    // 标签
    private JLabel jLabel = new JLabel("更新图书");
    private JLabel jLabel2 = new JLabel("请输入书号：");
    private JLabel jLabel3 = new JLabel(" 类 别：");
    private JLabel jLabel4 = new JLabel();
    private JLabel jLabel5 = new JLabel(" 作 者：");
    private JLabel jLabel6 = new JLabel();
    private JLabel jLabel7 = new JLabel(" 出版社:");
    private JLabel jLabel8 = new JLabel();
    private JLabel jLabel9 = new JLabel(" 书 名：");
    private JLabel jLabel10 = new JLabel();

    // 文本框
    private JTextField field = new JTextField(20);
    private JTextField jTextField1 = new JTextField();
    private JTextField jTextField2 = new JTextField();
    private JTextField jTextField3 = new JTextField();

    //下拉栏
    private JComboBox<String> box = new JComboBox<String>();

    // 大小
    // private Dimension dimension = new Dimension(220, 20);
    // 按钮
    private JButton button = new JButton("检索");
    private JButton button2 = new JButton("确认更新");
    private JButton button3 = new JButton("返回");

    // 面板
    private JPanel jPanel = new JPanel();
    // 字体
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font2 = new Font("宋体", Font.BOLD, 30);
    private Font font3 = new Font("宋体", Font.BOLD, 26);

//    private JLabel jLabel = new JLabel("更新图书");
//    private JLabel jLabel1 = new JLabel(" 类别 :");
//    private JLabel jLabel2 = new JLabel(" 书名 :");
//    private JLabel jLabel3 = new JLabel(" 作者 :");
//    private JLabel jLabel4 = new JLabel("出版社:");
//    private JLabel jLabel5 = new JLabel(" 书号 :");
//
//    private JComboBox<String> box = new JComboBox<String>();
//
//    private JTextArea jTextArea1 = new JTextArea();
//    private JTextArea jTextArea2 = new JTextArea();
//    private JTextArea jTextArea3 = new JTextArea();
//    private JTextArea jTextArea4 = new JTextArea();
//
//    private JButton button1 = new JButton("确认修改");
//    private JButton button2 = new JButton("返回");
//
//    private Font font1 = new Font("宋体",Font.BOLD,60);
//    private Font font2 = new Font("宋体",Font.BOLD,40);
//    private Font font3 = new Font("宋体",Font.BOLD,30);
//
//    private JPanel jPanel1 = new JPanel();
//    private JPanel jPanel2 = new JPanel();
//    private JPanel jPanel3 = new JPanel();
//    private JPanel jPanel4 = new JPanel();
//    private JPanel jPanel5 = new JPanel();

    private UpdateBook() {
//        Icon i = new ImageIcon("./img/login2.jpg");
//        JLabel Label = new JLabel(i);
//        Label.setBounds(0, 0, 800, 800);
//
//        jLabel.setFont(font1);
//        jLabel.setBounds(280, 50, 300, 80);
//        jLabel.setForeground(Color.white);
//
//        //书号
//        jLabel5.setFont(font2);
//        jLabel5.setBounds(150, 190, 200, 50);
//        jLabel5.setForeground(Color.white);
//        //jTextArea1.setLayout(null);
//        jTextArea4.setPreferredSize(new Dimension(350,50));
//        jTextArea4.setBorder(BorderFactory.createLineBorder(Color.white,3));
//        jTextArea4.setFont(font2);
//        jTextArea4.setForeground(Color.black);
//        jPanel5.setBounds(0,150,800,60);
//        jPanel5.add(jLabel5);
//        jPanel5.add(jTextArea4);
//
//        //类别
//        jLabel1.setFont(font2);
//        jLabel1.setBounds(150,270,200,50);
//        jLabel1.setForeground(Color.white);
//        box.setPreferredSize(new Dimension(350,50));
//
//        ArrayList<String> typeList = AddBook.getTypeList();
//        for(String type: typeList){
//            box.addItem(type);
//        }
//
//        box.setFont(font3);
//        //box.setBounds(400,200,200,40);
//        box.setOpaque(false);
//        jPanel1.setBounds(0,230,800,60);
//        jPanel1.add(jLabel1);
//        jPanel1.add(box);
//
//        //书名
//        jLabel2.setFont(font2);
//        jLabel2.setBounds(150, 360, 200, 50);
//        jLabel2.setForeground(Color.white);
//        //jTextArea1.setLayout(null);
//        jTextArea1.setPreferredSize(new Dimension(350,50));
//        jTextArea1.setBorder(BorderFactory.createLineBorder(Color.white,3));
//        jTextArea1.setFont(font2);
//        jTextArea1.setForeground(Color.black);
//        jPanel2.setBounds(0,320,800,60);
//        jPanel2.add(jLabel2);
//        jPanel2.add(jTextArea1);
//
//        //作者
//        jLabel3.setFont(font2);
//        jLabel3.setBounds(150, 420, 200, 50);
//        jLabel3.setForeground(Color.white);
//        //jTextArea1.setLayout(null);
//        jTextArea2.setPreferredSize(new Dimension(350,50));
//        jTextArea2.setBorder(BorderFactory.createLineBorder(Color.white,3));
//        jTextArea2.setFont(font2);
//        jTextArea2.setForeground(Color.black);
//        jPanel3.setBounds(0,410,800,60);
//        jPanel3.add(jLabel3);
//        jPanel3.add(jTextArea2);
//
//        //出版社
//        jLabel4.setFont(font2);
//        jLabel4.setBounds(150, 500, 200, 50);
//        jLabel4.setForeground(Color.white);
//        //jTextArea1.setLayout(null);
//        jTextArea3.setPreferredSize(new Dimension(350,50));
//        jTextArea3.setBorder(BorderFactory.createLineBorder(Color.white,3));
//        jTextArea3.setFont(font2);
//        jTextArea3.setForeground(Color.black);
//        jPanel4.setBounds(0,500,800,60);
//        jPanel4.add(jLabel4);
//        jPanel4.add(jTextArea3);
//
//        button1.setBounds(280,600,300,60);
//        button1.setFont(font2);
////        button1.setForeground(Color.white);
//        button2.setBounds(620,650,100,50);
////        button2.setForeground(Color.white);
//        button2.setFont(font3);
//
//        jPanel1.setOpaque(false);
//        jPanel2.setOpaque(false);
//        jPanel3.setOpaque(false);
//        jPanel4.setOpaque(false);
//        jPanel5.setOpaque(false);
//
//        jTextArea1.setOpaque(false);
//        jTextArea2.setOpaque(false);
//        jTextArea3.setOpaque(false);
//        jTextArea4.setOpaque(false);
//
//        add(jLabel);
//        add(jPanel1);
//        add(jPanel2);
//        add(jPanel3);
//        add(jPanel4);
//        add(jPanel5);
//        add(button1);
//        add(button2);
//        add(Label);
//
//        setSize(800,800);
//        setLocationRelativeTo(null);
//        setVisible(true);
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
//        button2.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                setVisible(false);
//                AdminInterface.getInstance().admin.setVisible(true);
//            }
//        });
//
//        button1.setFocusPainted(false);
//        button2.setFocusPainted(false);

        // 改变背景图片
        Icon i = new ImageIcon(UpdateBook.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        // 标签
        jLabel.setFont(font);
        jLabel.setBounds(280, 35, 800, 100);
        jLabel.setForeground(Color.white);

        jLabel2.setFont(font2);
        jLabel2.setBounds(80, 164, 250, 30);
        jLabel2.setForeground(Color.white);

        // 文本框
        field.setFont(font2);
        field.setBackground(Color.white);
        field.setBounds(280, 164, 250, 30);
        field.setForeground(Color.white);
        field.setOpaque(false);

        // 按钮
        button.setFont(font2);
        button.setBounds(580, 160, 100, 40);
        button.setForeground(Color.white);
        button.setBackground(Color.white);
        button.setOpaque(false);

        // 面板
        jPanel.setBounds(70, 220, 620, 450);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);

        // 面板上标签及按钮
        jLabel3.setFont(font2);
        //jLabel4.setFont(font2);
        jLabel5.setFont(font2);
        //jLabel6.setFont(font2);
        jLabel7.setFont(font2);
        //jLabel8.setFont(font2);
        jLabel9.setFont(font2);
        //jLabel10.setFont(font2);

        button2.setFont(font3);
        button3.setFont(font3);

        jLabel3.setForeground(Color.black);
        jLabel4.setForeground(Color.black);
        jLabel5.setForeground(Color.black);
        jLabel6.setForeground(Color.black);
        jLabel7.setForeground(Color.black);
        jLabel8.setForeground(Color.black);
        jLabel9.setForeground(Color.black);
        jLabel10.setForeground(Color.black);

        button2.setForeground(Color.black);
        button3.setForeground(Color.black);

        jLabel9.setBounds(40, 20, 150, 50);
        jTextField3.setBounds(200,20,400,50);
        //jLabel10.setBounds(200, 20, 400, 50);
        jLabel3.setBounds(40, 90, 150, 50);
        box.setBounds(200,90,400,50);
        //jLabel4.setBounds(200, 90, 400, 50);
        jLabel5.setBounds(40, 160, 150, 50);
        jTextField1.setBounds(200,160,400,50);
        //jLabel6.setBounds(200, 160, 400, 50);
        jLabel7.setBounds(40, 230, 150, 50);
        jTextField2.setBounds(200,230,400,50);
       // jLabel8.setBounds(200, 230, 400, 50);

        button2.setBounds(250, 310, 150, 50);
        button3.setBounds(500, 630, 90, 50);
        button2.setBackground(Color.white);
        button2.setOpaque(false);
        button3.setBackground(Color.white);
        button3.setOpaque(false);

        jTextField1.setFont(font2);
        jTextField2.setFont(font2);
        jTextField3.setFont(font2);
        box.setFont(font2);
        jTextField1.setOpaque(false);
        jTextField2.setOpaque(false);
        jTextField3.setOpaque(false);
        box.setOpaque(false);

        jPanel.add(jLabel3);
       // jPanel.add(jLabel4);
        jPanel.add(jLabel5);
       // jPanel.add(jLabel6);
        jPanel.add(jLabel7);
        //jPanel.add(jLabel8);
        jPanel.add(jLabel9);
        //jPanel.add(jLabel10);
        jPanel.add(button2);
        //jPanel.add(button3);
        jPanel.add(jTextField1);
        jPanel.add(box);
        jPanel.add(jTextField2);
        jPanel.add(jTextField3);
        jPanel.setVisible(false);

        jPanel.setOpaque(false);

        add(jLabel);
        add(jLabel2);
        add(field);
        add(button);
        add(jPanel);
        add(button3);
        add(Label);

        setSize(800,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jPanel.setVisible(true);
                        Book book = BorrowAndReturn.returnSearch(field.getText());
                        if(book != null){
                            jTextField3.setText(book.getBookName());
                            jTextField1.setText(book.getWriter());
                            jTextField2.setText(book.getPress());
                            ArrayList<String> typeList = AddBook.getTypeList();
                            for(String type: typeList){
                                box.addItem(type);
                            }
                            int i;
                            for(i = 0; i < typeList.toArray().length; i ++){
                                if(box.getItemAt(i).toString().equals(book.getType())){
                                    break;
                                }
                            }
                            box.setSelectedItem(box.getItemAt(i));

                        }
                    }
                }
        );

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bookId = field.getText();
                String bookName = jTextField3.getText();
                String writer = jTextField1.getText();
                String press = jTextField2.getText();
                String type = box.getSelectedItem().toString();
                updateBook(bookId, bookName, type, writer, press);
                ManageBook.getInstance().showBooks();
                removeInstance();
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button.setFocusPainted(false);
        button2.setFocusPainted(false);
        button3.setFocusPainted(false);
    }

    public static UpdateBook getInstance() {
        if (instance==null) instance=new UpdateBook();
        return instance;
    }

    public static void removeInstance(){
        instance = null;
    }

//    public static void main(String[] args) {
//        UpdateBook.getInstance();
//    }

    /**
     * @author zj
     * 在数据库中更新书籍
     */
    public void updateBook(String bookId, String bookName, String type, String writer, String press){
        if(bookName.length() == 0 || press.length() == 0 || writer.length() == 0){
            JOptionPane.showMessageDialog(null, "输入信息不能为空！");
            return;
        }
        String sql = "update book set bookname = ?, type = ?, writer = ?, press = ? where bookid = ?";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, bookName);
            preSql.setString(2, type);
            preSql.setString(3, writer);
            preSql.setString(4, press);
            preSql.setString(5, bookId);
            preSql.executeUpdate();
            JOptionPane.showMessageDialog(null, "更新图书成功");
        }catch (Exception e){
            System.out.println(e);
        }
        setVisible(false);
        AdminInterface.getInstance().admin.setVisible(true);
    }
}
